﻿using MyCode.Project.Repositories.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyCode.Project.Domain.Message;
using MyCode.Project.Domain.Model;
using MyCode.Project.Domain.Repositories;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Search;
using MyCode.Project.Infrastructure.Enumeration;
using MyCode.Project.Infrastructure.Extensions;

namespace MyCode.Project.Repositories
{
    public class SysWorkProcessV2Repository: Repository<SysWorkProcessV2>, ISysWorkProcessV2Repository
    {
        public SysWorkProcessV2Repository(MyCodeSqlSugarClient context) : base(context)
        { }



        #region Exists(是否存在调度)
        /// <summary>
        /// 是否存在调度
        /// </summary>
        /// <param name="funcType">执行类型</param>
        /// <param name="merchantId">商家ID</param>
        /// <param name="funcClass">执行类</param>
        /// <param name="funcMethod">执行方法名</param>
        /// <param name="paramInfo">参数信息</param>
        /// <returns></returns>
        public bool Exists(FuncType funcType, Guid merchantId, string funcClass, string funcMethod, string paramInfo)
        {
            var sql = @"select COUNT(1) from SysWorkProcessV2 with(nolock)";
            SearchCondition where = new SearchCondition();
            where.AddCondition("MerchantID", merchantId, SqlOperator.Equal, true)
                .AddCondition("FuncType", funcType.Value(), SqlOperator.Equal, true)
                .AddCondition("FuncClass", funcClass, SqlOperator.Equal, true)
                .AddCondition("FuncMethod", funcMethod, SqlOperator.Equal, true)
                .AddCondition("ParamInfo", paramInfo, SqlOperator.Equal, true)
                .AddSqlCondition($@" FuncStatus = { FuncStatus.Running.Value()} or FuncStatus={FuncStatus.ExceptionStop.Value()} ", true);
            var result = this.SelectFirst<int>(sql, where);
            return result > 0;
        }

        #endregion


    }
}